package xuegao.study.mall.rbac.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsAdmin;
import xuegao.study.mall.common.model.ums.UmsAdminLoginLog;
import xuegao.study.mall.rbac.service.UmsAdminLoginLogService;
import xuegao.study.mall.rbac.service.UmsAdminService;
import xuegao.study.mall.repo.dao.ums.UmsAdminLoginLogDao;

import java.util.Date;

@Service
public class UmsAdminLoginLogServiceImpl implements UmsAdminLoginLogService {

    private Logger logger = LoggerFactory.getLogger(UmsAdminLoginLogServiceImpl.class);

    @Autowired
    private UmsAdminService umsAdminService;

    @Autowired
    private UmsAdminLoginLogDao umsAdminLoginLogDao;

    @Override
    public CommonResult insertLog(String username) {
        UmsAdmin admin = umsAdminService.getAdminByUsername(username);
        if (null != admin) {
            UmsAdminLoginLog log = new UmsAdminLoginLog();
            log.setAdminId(admin.getId());
            log.setCreateTime(new Date());
            log.setIp("127.0.0.0");
            umsAdminLoginLogDao.insert(log);
            logger.warn("用户登录:{}", log);
            return CommonResult.success(log);
        }
        return CommonResult.failed();
    }
}
